package Datebase;
import javax.swing.table.DefaultTableCellRenderer;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.text.StyledDocument;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class tselemassage extends JFrame {

        JFrame frame3;
        public static void main(String[] args) {
            System.setProperty("sun.java2d.noddraw", "true");
            new tselemassage();
        }
        public tselemassage()
        {
            frame3 = new JFrame("学生信息查询系统");
            //图标
            URL resource = tselemassage.class.getClassLoader().getResource("kun.png");
            Image image = new ImageIcon(resource).getImage();
            frame3.setIconImage(image);
            //窗口尺寸
            frame3.setSize(800, 600);
            frame3.setLocationRelativeTo(null);
            frame3.setVisible(true);

            frame3.setLayout(null);
            JLabel sele = new JLabel("请输入学号:");

            sele.setForeground(new Color(0x0A0A0A));
            sele.setFont(new Font("黑体", Font.PLAIN, 18));
            sele.setBounds(150, 50, 250, 40);
            frame3.add(sele);
//添如用户名输入帼
            JTextField use = new JTextField(20);
            use.setFont(new Font("黑体", Font.PLAIN, 18));
            use.setSelectedTextColor(new Color(0x0A0A0A));
            use.setBounds(330, 50, 200, 40);
            frame3.add(use);

            JButton button6 = new JButton("查询");
            button6.setFont(new Font("黑体", Font.PLAIN, 15));
            button6.setBounds(580, 50, 100, 40);
            frame3.add(button6);


            frame3.repaint();
//创建表格
            button6.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {


                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
                        String user = "HR";
                        String password = "root";

                        Connection conn = DriverManager.getConnection(dbURL, user, password);
                        String sql = "select * from STUDENT where SNO = '" + use.getText() + "'";
                        PreparedStatement pstm = conn.prepareStatement(sql);
                        ResultSet rs = pstm.executeQuery();

                        int count = 0;
                        while (rs.next()) {
                            count++;
                        }

                        if(count == 0)
                        {
                            JOptionPane.showMessageDialog(null, "学号不存在",
                                    "提示", JOptionPane.WARNING_MESSAGE);
                        }
                        else {
                            rs = pstm.executeQuery();

                            Object[][] data = new Object[count][7];
                            String[] title = {"学号", "姓名", "性别", "系", "专业", "班级"};
                            count = 0;
                            while (rs.next()) {
                                data[count][0] = (rs.getString("SNO"));
                                data[count][1] = rs.getString("SNAME");
                                data[count][2] = rs.getString("SSEX");
                                data[count][3] = rs.getString("DEPARTMENT");
                                data[count][4] = rs.getString("SD");
                                data[count][5] = rs.getString("CLASS");
                                count++;

                            }
                            JTable table = new JTable(data, title);
                            JScrollPane scrollPane = new JScrollPane(table);
                            DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
                            cr.setHorizontalAlignment(SwingConstants.CENTER);
                            table.setDefaultRenderer(Object.class, cr);

                            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                            table.getColumnModel().getColumn(0).setPreferredWidth(300);
                            table.getColumnModel().getColumn(1).setPreferredWidth(200);
                            table.getColumnModel().getColumn(2).setPreferredWidth(200);
                            table.getColumnModel().getColumn(3).setPreferredWidth(200);
                            table.getColumnModel().getColumn(4).setPreferredWidth(200);
                            table.getColumnModel().getColumn(5).setPreferredWidth(200);


                            table.setRowHeight(70);
                            table.setFont(new Font("微软雅黑", Font.BOLD, 17));
                            scrollPane.setBounds(70, 120, 650, 93);
                            frame3.add(scrollPane);
                        }


                    } catch (ClassNotFoundException cnfe) {
                        cnfe.printStackTrace();
                        JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                    } catch (SQLException sqle) {
                        sqle.printStackTrace();
                        JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
                    }

                }
            });
            frame3.setVisible(true);

        }
    }
